package com.system.travel.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.system.travel.modules.system.entity.SysUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 系统用户Mapper接口
 *
 * @author lucas
 * @date 2024/01/01
 */
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
     * 通过用户名查询用户
     */
    @Select("select * from sys_user where username = #{username} and del_flag = 0")
    SysUser selectByUsername(@Param("username") String username);

    /**
     * 查询用户的所有权限
     */
    @Select("select distinct p.* from sys_user_role ur " +
            "left join sys_role_permission rp on ur.role_id = rp.role_id " +
            "left join sys_permission p on rp.permission_id = p.id " +
            "where ur.user_id = #{userId}")
    List<String> selectPermissionsByUserId(@Param("userId") Long userId);

    /**
     * 查询用户的所有角色
     */
    @Select("select r.role_code from sys_user_role ur " +
            "left join sys_role r on ur.role_id = r.id " +
            "where ur.user_id = #{userId}")
    List<String> selectRolesByUserId(@Param("userId") Long userId);
} 